package com.slz3.business.module.systemManagement.sysRoleMenu.service;

import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.util.UUIDUtils;
import com.slz3.business.module.systemManagement.sysRoleMenu.domain.SysRoleMenu;
import com.slz3.business.module.systemManagement.sysRoleMenu.domain.SysRoleMenuFactory;
import com.slz3.business.module.systemManagement.sysRolePermissions.domain.SysRolePermissions;
import com.slz3.business.module.systemManagement.sysRolePermissions.domain.SysRolePermissionsFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SysRoleMenuTransactionalServiceImpl implements SysRoleMenuTransactionalService {

    @Override
    public SysRoleMenu modify(SysRoleMenu sysRoleMenuNew) {
        Repositorys.sysRoleMenu.test();
        Repositorys.sysRoleMenu.save(SysRoleMenuFactory.initBaseFields());
        System.err.println(1/0);
        sysRoleMenuNew.setLastModifyTime(LocalDateTime.now());
        return Repositorys.sysRoleMenu.saveAndFlush(sysRoleMenuNew);
    }

    @Override
    public List<SysRoleMenu> batchModify(List<SysRoleMenu> sysRoleMenuList) {
        return Repositorys.sysRoleMenu.saveAll(sysRoleMenuList);
    }

    @Override
    public boolean save(SysRoleMenu sysRoleMenu) {

        if (!CollectionUtils.isEmpty(sysRoleMenu.getUnSelectMenuUuids())){
            sysRoleMenu.getUnSelectMenuUuids()
                    .forEach(item -> Repositorys.sysRoleMenu.deleteByRoleUuidAndMenuUuid(sysRoleMenu.getRoleUuid(), item));
        }

        if (!CollectionUtils.isEmpty(sysRoleMenu.getSelectMenuUuids())){
            Repositorys.sysRoleMenu.saveAll(sysRoleMenu.getSelectMenuUuids().stream().map(item -> {
                Repositorys.sysRoleMenu.deleteByRoleUuidAndMenuUuid(sysRoleMenu.getRoleUuid(), item);
                SysRoleMenu wantAdd = new SysRoleMenu();
                wantAdd.setUuid(UUIDUtils.generateUUID());
                wantAdd.setMenuUuid(item);
                wantAdd.setRoleUuid(sysRoleMenu.getRoleUuid());
                wantAdd.setCreator(sysRoleMenu.getCreator());
                return wantAdd;
            }).collect(Collectors.toList()));
        }

        Repositorys.sysRolePermissions.deleteByRoleUuidAndMenuUuid(sysRoleMenu.getRoleUuid(), sysRoleMenu.getMenuUuid());
        if (!CollectionUtils.isEmpty(sysRoleMenu.getSelectPermissionUuid())){
            Repositorys.sysRolePermissions.saveAll(
                sysRoleMenu.getSelectPermissionUuid()
                    .stream()
                    .map(item -> {
                        SysRolePermissions sysRolePermissions = SysRolePermissionsFactory.initBaseFields();
                        sysRolePermissions.setRoleUuid(sysRoleMenu.getRoleUuid());
                        sysRolePermissions.setMenuUuid(sysRoleMenu.getMenuUuid());
                        sysRolePermissions.setPermissionUuid(item);
                        sysRolePermissions.setCreator(sysRoleMenu.getCreator());
                        return sysRolePermissions;
                    }).collect(Collectors.toList())

            );
        }
        return true;
    }
}
